package com.gee.spring.boot.oauth2.auth.config.oauth2.token;

import com.gee.spring.boot.oauth2.auth.entity.AuthUser;
import org.springframework.security.oauth2.common.DefaultOAuth2AccessToken;
import org.springframework.security.oauth2.common.OAuth2AccessToken;
import org.springframework.security.oauth2.provider.OAuth2Authentication;
import org.springframework.security.oauth2.provider.token.TokenEnhancer;

import java.util.HashMap;
import java.util.Map;

/**
 * desc:
 *
 * @author gee
 * @date 2020/04/06 16:56
 */
public class JwtTokenEnhancer implements TokenEnhancer {
    @Override
    public OAuth2AccessToken enhance(OAuth2AccessToken oAuth2AccessToken, OAuth2Authentication oAuth2Authentication) {
        AuthUser authUser = (AuthUser)oAuth2Authentication.getUserAuthentication().getPrincipal();
        final Map<String,Object> userInfo = new HashMap<>(2);
        userInfo.put("userId",authUser.getId());
        userInfo.put("username",authUser.getUsername());
        ((DefaultOAuth2AccessToken)oAuth2AccessToken).setAdditionalInformation(userInfo);
        return oAuth2AccessToken;
    }
}
